package com.ellricky.baselib.widget;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ellricky.baselib.BaseConstant;
import com.ellricky.baselib.R;
import com.ellricky.baselib.entity.PersonItemEntity;
import com.ellricky.baselib.ui.PersonChooseActivity;
import com.ellricky.baselib.adapter.PersonGridAdapter;
import com.ellricky.baselib.adapter.PersonItemDecoration;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class EditAttendView extends FrameLayout implements AdapterView.OnItemClickListener {

    private Activity activity;

    private TextView tvKey;
    private RecyclerView attendRv;
    private PersonGridAdapter personGridAdapter;
    private List<PersonItemEntity> attendItemList;

    private String ids;
    private String apiPath, classId;
    private boolean showSex;

    public EditAttendView(Context context) {
        super(context);
        initView(context);
    }

    public EditAttendView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public EditAttendView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public void setTvKey(String key) {
        tvKey.setText(key);
    }

    private void initView(Context context) {
        activity = (Activity) context;

        View.inflate(context, R.layout.base_layout_edit_attend, this);

        tvKey = findViewById(R.id.tvKey);
        attendRv = findViewById(R.id.rvAttend);

        GridLayoutManager glm = new GridLayoutManager(context, 3) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };

        attendRv.setLayoutManager(glm);
        attendRv.addItemDecoration(new PersonItemDecoration(activity, 10));
        attendRv.setNestedScrollingEnabled(false);
//        attendRv.setHasFixedSize(true);

        personGridAdapter = new PersonGridAdapter(activity);
        personGridAdapter.setOnItemClickListener(this);
        attendRv.setAdapter(personGridAdapter);
    }


    public void initApiPath(String apiPath, String classId) {
        this.apiPath = apiPath;
        this.classId = classId;
    }


    public String getIds() {
        return ids;
    }


    public void initDataWithNull(boolean showSex) {
        updateAdapterData(null, showSex);
    }

    public void updateDataWithSex(List<PersonItemEntity> attendEntityList) {
        updateAdapterData(attendEntityList, true);
    }

    public void updateDataWithoutSex(List<PersonItemEntity> attendEntityList) {
        updateAdapterData(attendEntityList, false);
    }

    private void updateAdapterData(List<PersonItemEntity> entityList, boolean showSex) {
        this.showSex = showSex;

        this.attendItemList = entityList;
        if (attendItemList == null) attendItemList = new ArrayList<>();

        List<PersonItemEntity> itemList = new ArrayList<>(attendItemList);

        StringBuilder builder = new StringBuilder();
        for (PersonItemEntity entity : itemList) {
            builder.append(entity.getId()).append(",");
        }
        if (builder.length() > 0)
            builder = builder.deleteCharAt(builder.lastIndexOf(","));

        this.ids = builder.toString();
        Log.e("attend ,ids== >", ids);


        itemList.add(null);

        personGridAdapter.setShowSex(showSex);
        personGridAdapter.refresh(itemList);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        PersonItemEntity itemEntity = (PersonItemEntity) personGridAdapter.getItem(i);
        if (itemEntity == null) {
            Intent intent = new Intent(activity, PersonChooseActivity.class);
            intent.putExtra(BaseConstant.KEY_PERSON_LIST, (Serializable) attendItemList);
            intent.putExtra("api_path", apiPath);
            intent.putExtra("class_id", classId);
            intent.putExtra("showSex", showSex);
            activity.startActivityForResult(intent, 0);
        }
    }
}
